package cn.wuxing.mediation.web.controller.reception;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.wuxing.mediation.system.domain.ShiftScheduling;
import cn.wuxing.mediation.system.domain.VisitingReception;
import cn.wuxing.mediation.system.domain.vo.ShiftVo;
import cn.wuxing.mediation.system.domain.vo.VisitingVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.wuxing.mediation.common.annotation.Log;
import cn.wuxing.mediation.common.core.controller.BaseController;
import cn.wuxing.mediation.common.core.domain.AjaxResult;
import cn.wuxing.mediation.common.enums.BusinessType;
import cn.wuxing.mediation.system.service.IVisitingReceptionService;
import cn.wuxing.mediation.common.utils.poi.ExcelUtil;
import cn.wuxing.mediation.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 考察接待Controller
 *
 * @author mediation
 * @date 2024-10-12
 */
@RestController
@RequestMapping("/system/reception")
public class VisitingReceptionController extends BaseController {
    @Resource
    private IVisitingReceptionService visitingReceptionService;

    /**
     * 查询考察接待列表
     */
    @GetMapping("/list")
    public TableDataInfo list(VisitingReception visitingReception) {
        startPage();
        List<VisitingReception> list = visitingReceptionService.selectVisitingReceptionList(visitingReception);
        return getDataTable(list);
    }

    /**
     * 导出考察接待列表
     */
    @Log(title = "考察接待", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VisitingReception visitingReception) {
        List<VisitingReception> list = visitingReceptionService.selectVisitingReceptionList(visitingReception);
        list.forEach(i -> {
            if (i.getReceptionStatus() != null) {
                if (!i.getReceptionStatus().equals("")) {
                    if (i.getReceptionStatus().equals("0")) {
                        i.setReceptionStatus("未接待");
                    }
                    if (i.getReceptionStatus().equals("1")) {
                        i.setReceptionStatus("已接待");
                    }
                }
            }
        });
        ExcelUtil<VisitingReception> util = new ExcelUtil<VisitingReception>(VisitingReception.class);
        util.exportExcel(response, list, "考察接待数据");
    }

    /**
     * 获取考察接待详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(visitingReceptionService.selectVisitingReceptionById(id));
    }

    /**
     * 新增考察接待
     */
    @Log(title = "考察接待", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VisitingReception visitingReception) {
        return toAjax(visitingReceptionService.insertVisitingReception(visitingReception));
    }

    /**
     * 修改考察接待
     */
    @Log(title = "考察接待", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VisitingReception visitingReception) {
        return toAjax(visitingReceptionService.updateVisitingReception(visitingReception));
    }

    /**
     * 删除考察接待
     */
    @Log(title = "考察接待", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(visitingReceptionService.deleteVisitingReceptionByIds(ids));
    }

    /**
     * 下载导入模板
     *
     * @param response
     */
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<VisitingVo> util = new ExcelUtil<VisitingVo>(VisitingVo.class);
        util.importTemplateExcel(response, "考察接待");
    }

    /**
     * 导入
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:scheduling:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<VisitingReception> util = new ExcelUtil<VisitingReception>(VisitingReception.class);
        List<VisitingReception> visitingReceptionList = util.importExcel(file.getInputStream());
        String message = visitingReceptionService.importShiftScheduling(visitingReceptionList, updateSupport);
        return success(message);
    }
}
